//
//  InfoArchiver.m
//  YYTextTest
//
//  Created by macintosh on 16/9/13.
//  Copyright © 2016年 macintosh. All rights reserved.
//

#import "InfoArchiver.h"

@implementation InfoArchiver

+ (NSData *)archivedDataWithRootObject:(id)rootObject
{
    if (!rootObject) return nil;
    NSMutableData *data = [NSMutableData data];
    InfoArchiver *archiver = [[[self class] alloc] initForWritingWithMutableData:data];
    [archiver encodeRootObject:rootObject];
    [archiver finishEncoding];
    return data;
}

+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path
{
    NSData *data = [self archivedDataWithRootObject:rootObject];
    if (!data) return NO;
    return [data writeToFile:path atomically:YES];
}

@end

@implementation InfoUnarchiver

+ (id)unarchiveObjectWithData:(NSData *)data
{
    if (data.length == 0) return nil;
    InfoUnarchiver *unarchiver = [[self alloc] initForReadingWithData:data];
    return [unarchiver decodeObject];
}

+ (id)unarchiveObjectWithFile:(NSString *)path
{
    NSData *data = [NSData dataWithContentsOfFile:path];
    return [self unarchiveObjectWithData:data];
}

@end